<h1>设置界面</h1>
<div class="layui-form">
    <form enctype="multipart/form-data" method="post" name="fileinfo">
        <label class="layui-form-label " id="uploadBtn" type="button">选择头像</label>
        <div class="layui-input-inline">
            <input type="file" id="goodsImg" name="headerImg" required lay-verify="required" placeholder="文件名"
                   autocomplete="off" class="layui-input"/>
            <input type="submit" class="layui-btn-normal" >换个头像</input>
        </div>
    </form>
</div>

<script>


    Date.prototype.Format = function (format) {
        var o = {
            "M+": this.getMonth() + 1,
            "d+": this.getDate(),
            "H+": this.getHours(),
            "m+": this.getMinutes(),
            "s+": this.getSeconds(),
            "S+": this.getMilliseconds()
        };
        if (/(y+)/.test(format)) {
            format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
        }
        for (var k in o) {
            if (new RegExp("(" + k + ")").test(format)) {
                format = format.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
            }
        }
        return format;
    };

    layui.use(['element', 'form', 'layer'], function () {
        var layer = layui.layer,
            form = layui.form,
            element = layui.element,
            $ = layui.jquery;
        console.log("into setting!!!!!!!!!!!!!!");
            var f = document.forms.namedItem("fileinfo");;
            f.addEventListener('submit',function (ev) {
                var data = new FormData(f);
                data.append('timeModified', new Date().Format("yyyy-MM-dd HH:mm:ss"));
                $.ajax({
                    url: '/api/user/'+localStorage.getItem('userId')
                    , type: 'POST'
                    ,enctype: 'multipart/form-data'
                    , data: data
                    , processData: false
                    , contentType: false
                    , cache: false
                    , timeout: 6000
                    , success: function (ret) {
                        if (ret.code === 0) {
                            layer.msg('更新成功');
                            localStorage.setItem("headerImg",ret.data);
                            console.log(ret.data);
                            //更新头像
                        }
                        if (ret.code === 2 || ret.code === 3) {
                            layer.msg(ret.msg);
                            logout();
                        }
                    }
                    , error: function () {
                        console.log("更新头像ajax请求错误");
                    }
                });
                ev.preventDefault();
            },false)

        });

    function logout() {
        console.log("退出.........");
        localStorage.removeItem("prefix");
        localStorage.removeItem("userId");
        localStorage.removeItem("nickname");
        localStorage.removeItem("headImg");
        location.href = '/html/login.html'
    }
</script>